package com.xzmzhp.security;

import com.xzmzhp.dao.people.PersonnelMapper;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.vo.personnel.PersonnelVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 王源(xm)
 * @Date: 2020年12月10日02:49:45
 * @Description: 授权过滤器
 */
@Component
public class TokenAuthenticationFilter extends OncePerRequestFilter {

    /**
     * Token工具类
     */
    @Autowired
    private TokenManager tokenManager;

    /**
     * 人员信息操作类
     */
    @Autowired
    private PersonnelMapper personnelMapper;

    @Override
    protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain chain)
            throws ServletException, IOException {
        String token = tokenManager.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            //从token中获取用户ID
            String studentId = tokenManager.getUserInfoFromToken(response,token);
            //根据用户ID查询用户信息
            PersonnelVo personnelVo = personnelMapper.findByStudentId(studentId);
            //初始化登录用户类
            LoginUser loginUser = new LoginUser();
            //添加用户信息
            loginUser.setUser(personnelVo);
            loginUser.setToken(token);
            loginUser.setLoginTime(System.currentTimeMillis());
            //初始化权限类
            List<String> permissionValueList = new ArrayList<>();
            //添加权限信息
            permissionValueList.add(personnelVo.getCompetenceName());
            //将目录信息放到上下文中
            Collection<GrantedAuthority> authority = new ArrayList<>();
            for (String permissionValue : permissionValueList) {
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(permissionValue);
                authority.add(auth);
            }
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        chain.doFilter(request, response);
    }
}
